<?php

include_once("../../php/db/config.php");

class FUIServices { 
	var $dbhost = HOSTNAME;
	var $dbname = DATABASE;
	var $dbuser = USERNAME;
	var $dbpass = PASSWORD;
	var $link;
	
	function FUIServices()
	{
	    $this->methodTable = array(
			"get_player_by_name" => array(
					"description" => "query for player data by name",
					"access"    => "remote",
					"arguments" => array(arg1),
					"returntype"=> "mysqli"
					),
			"get_player_data_by_id" => array(
					"description" => "query for player data by id",
					"access"    => "remote",
					"arguments" => array(arg1),
					"returntype"=> "mysqli"
					),
			"verify_user_by_name" => array(
					"description" => "query for player data by id",
					"access"    => "remote",
					"arguments" => array(arg1),
					"returntype"=> "recordSet"
					),
			"get_langs_list" => array(
					"description" => "get list of supported languages",
					"access"    => "remote",
					"arguments" => null,
					"returntype"=> "mysqli"
					),
			"get_game_info" => array(
					"description" => "get list of supported languages",
					"access"    => "remote",
					"arguments" => array(arg1),
					"returntype"=> "mysqli"
					),
			"add_new_player_wrapper" => array(
				    "description" => "add new player",
				    "access"    => "remote",
				    "arguments" => array(arg1),
				    "returntype"=> "mysqli"
					),
			
			"get_templates_list" => array(
					"description"    => "Get template list",
				    "access"    => "remote",
				    "arguments" => null,
				    "returns"=> "mysqli"
					),
			"get_classes_list" => array(
					"description"    => "Get class names and indexes list",
				    "access"    => "remote",
				    "arguments" => null,
				    "returns"=> "mysqli"
					)
			);
		$this->link = mysqli_init();
		
		if (!$this->link) {
			die('mysqli_init failed');
		}

		if (!mysqli_options($this->link, MYSQLI_INIT_COMMAND, 'SET AUTOCOMMIT = 0')) {
			die('Setting MYSQLI_INIT_COMMAND failed');
		}

		if (!mysqli_options($this->link, MYSQLI_OPT_CONNECT_TIMEOUT, 5)) {
			die('Setting MYSQLI_OPT_CONNECT_TIMEOUT failed');
		}	

		if (!mysqli_real_connect($this->link, $this->dbhost, $this->dbuser, $this->dbpass, $this->dbname)) {
			die('Connect Error (' . mysqli_connect_errno() . ') '
            . mysqli_connect_error());
		}	
	}
	
	function get_classes_list(){
		$query = "SELECT * FROM fui_components";

		$res = mysqli_real_query($this->link,$query);
		if ($res = mysqli_store_result($this->link)) 
		{
			return $res;
		}
		return mysqli_error($this->link);
	}
	function get_player_data_by_id($id){
		$query = "SELECT * FROM users WHERE id='$id'";

		$res = mysqli_real_query($this->link,$query);
		if ($res = mysqli_store_result($this->link)) 
		{
			return $res;
		}
		return mysqli_error($this->link);
	}
	function verify_user_by_name($user)
	{
		$name = $user[0];
		$pwd = $user[1];
		$query = "SELECT * FROM users WHERE `name`='$name' AND `passwd`=PASSWORD('$pwd')";
		$res = mysqli_real_query($this->link,$query);
		if ($res = mysqli_store_result($this->link)) 
		{
			return $res;
		}
		return mysqli_error($this->link);
		
	} 
	function get_game_info($gid)
	{
		$query = "call mp_get_game_info($gid)";
		if (mysqli_multi_query($this->link, $query)) 
		{
			$counter = 0;
			//do {
			if ($res = mysqli_store_result($this->link)) 
			{
					//while ($row = mysqli_fetch_assoc($res))
					//{
				//		$resarr[$counter] = $row;
				//		$counter++;
				//	}
					//mysqli_free_result($res);
				return $res;
			}
				//} while (mysqli_more_results($this->conn) && mysqli_next_result($this->conn));
				
		}	 
			
		return mysqli_error($this->link);
	}
	
	function get_player_by_name($name) {
		$query = "SELECT * FROM users WHERE name='$name'";
		$res = mysqli_real_query($this->link,$query);
		if ($res = mysqli_store_result($this->link)) 
		{
			return $res;
		}
		return mysqli_error($this->link);
	}
	function get_langs_list(){
		$query = "SELECT * FROM languages";
		
		$res = mysqli_real_query($this->link,$query);
		if ($res = mysqli_store_result($this->link)) 
		{
			return $res;
		}
		return mysqli_error($this->link);
	}
		
	function add_new_player_wrapper($user_data)
	{
	
		$query_new = "INSERT INTO `users` (`id` ,`name` ,`passwd` ,`logo` ,`f_name` ,`l_name` ,`gender` ,`age` ,
						`adrs_country` ,`adrs_city` ,`adrs_street` ,`adrs_zip` ,`lang_id` ,`email` ,`phone1` ,`phone2` ,
						`reg_date` ,`status`)
					  VALUES (NULL , '".$user_data[0]['name']."', PASSWORD('".$user_data[0]['password'].
					  "'), NULL , '".$user_data[0]['fname']."', '".$user_data[0]['lname']."', '".$user_data[0]['gender']."', '".$user_data[0]['age'].
					  "', '".$user_data[0]['country']."', '".$user_data[0]['city']."', '".$user_data[0]['street']."', '".$user_data[0]['zipcode'].
					  "', '".$user_data[0]['lang']."', '".$user_data[0]['email']."', '".$user_data[0]['phone']."', '".$user_data[0]['phone2']."',
CURRENT_TIMESTAMP , 'PENDING');";
		
		return mysqli_real_query($this->link, $query_new);
	}
	
	
	function get_templates_list()
	{
	
		$query = "call mp_get_tamplates_list";
		if (mysqli_multi_query($this->link, $query)) 
		{
			$counter = 0;
			//do {
			if ($res = mysqli_store_result($this->link)) 
			{
					//while ($row = mysqli_fetch_assoc($res))
					//{
				//		$resarr[$counter] = $row;
				//		$counter++;
				//	}
					//mysqli_free_result($res);
				return $res;
			}
				//} while (mysqli_more_results($this->conn) && mysqli_next_result($this->conn));
				
		}	 
			
		return mysqli_error($this->link);
	}
}
?>
